package naruto.datastructures.graph;

import java.util.*;
import naruto.common.GraphCommon;
import naruto.datastructures.instruction.*;

/**
 *
 * @author nacuong
 */
public class IfNode extends IfCompleteNode<IfInstruction>{
  public IfNode(IfInstruction ifInstruction){
    super(ifInstruction);
  }

  public IfNode(IfInstruction ifInstruction, Vector<Edge> ifEdges){
    super(ifInstruction, ifEdges);
  }

  /**
   * Check whether this if node contains a loop instruction
   * @return retrue if this node contains a loop instruction; false otherwise
   */
  public boolean isLoopNode(){
    return this.instruction.isLoopInstruction();
  }

  /**
   * Get the corresponding endif node of thid if node
   * @param model
   * @return
   */
  public EndIfNode getEndIfNode(IfCompleteModel model){
    EndIfInstruction ifInstruction = new EndIfInstruction(this.getInstruction().getInstruction());
    EndIfNode endIfNode = (EndIfNode) model.getNode(ifInstruction);
    return endIfNode;
  }

  /**
   * Check whether the if node has an outcoming edge with true attribute.
   * @return true if if node has a true outcoming edge; false otherwise
   */
  public boolean traverseTrueBranch(){
    for (Edge edge: this.getOutcomingEdges()){
      if (edge.getAttribute().equals(GraphCommon.EdgeAttribute.TRUE))
        return true;
    }
    return false;
  }

  /**
   * Check whether the if node has an outcoming edge with true attribute.
   * @return true if if node has a true outcoming edge; false otherwise
   */
  public boolean traverseFalseBranch(){
    for (Edge edge: this.getOutcomingEdges()){
      if (edge.getAttribute().equals(GraphCommon.EdgeAttribute.FALSE))
        return true;
    }
    return false;
  }
}
